package org.example.B1;

import org.example.B1.action.*;
import org.example.B1.condition.GuardCondition;
import com.github.oxo42.stateless4j.StateMachine;
import com.github.oxo42.stateless4j.StateMachineConfig;

/**
 * 恒温烧水器
 */
public class ThermostaticWaterTank {

    private StateMachineConfig<State, Event> config;
    private GuardCondition guardCondition = new GuardCondition();
    private StateMachine<State, Event> stateMachine;

    public ThermostaticWaterTank() {
        init();
    }

    public void init() {
        // 初始状态：水箱不为空
        guardCondition.setEmpty(false);

        // 创建状态机配置
        config = new StateMachineConfig<>();

        // 定义状态转换规则
        // 待机状态
        //1. 如果温度低于20°C且水箱不为空，则转换到加热状态
        //2. 如果温度低于20°C但水箱为空，则状态不变
        //3. 晚上11点整，转换到休眠状态
        config.configure(State.STANDBY)
                .permitIf(Event.startHeating, State.HEAT, guardCondition.hasWater(), new StartHeatingAction())
                .ignoreIf(Event.startHeating, guardCondition.isEmpty())
                .permit(Event.sleep, State.SLEEP, new SleepAction())
                .ignore(Event.completeRepair)
                .ignore(Event.toStandby)
                .ignore(Event.powerOffHigh)
                .ignore(Event.burningOut);


        // 加热状态
        //1. 如果温度高于100°C，则转换到待机状态
        //2. 水箱故障，则转换到维修状态
        //3. 晚上11点，转换到休眠状态
        config.configure(State.HEAT)
                .permit(Event.powerOffHigh, State.STANDBY, new PowerOffHighAction())
                .permit(Event.burningOut, State.REPAIR, new BurningOutAction())
                .permit(Event.sleep, State.SLEEP, new SleepAction())
                .ignore(Event.completeRepair)
                .ignore(Event.startHeating)
                .ignore(Event.toStandby);

        // 休眠状态
        //1. 早上七点，转换到待机状态
        config.configure(State.SLEEP)
                .permit(Event.toStandby, State.STANDBY,new ToStandbyAction())
                .ignore(Event.completeRepair)
                .ignore(Event.startHeating)
                .ignore(Event.powerOffHigh)
                .ignore(Event.burningOut)
                .ignore(Event.sleep);

        // 维修状态
        //1. 维修完成后，转换到待机状态
        config.configure(State.REPAIR)
                .permit(Event.completeRepair, State.STANDBY, new CompleteRepairAction())
                .ignore(Event.startHeating)
                .ignore(Event.toStandby)
                .ignore(Event.powerOffHigh)
                .ignore(Event.burningOut)
                .ignore(Event.sleep);

        // 创建状态机实例，初始状态为待机状态
        stateMachine = new StateMachine<>(State.STANDBY, config);
    }

    public void setEmptyWater(boolean empty) {
        this.guardCondition.setEmpty(empty);
    }

    public void fire(Event event) {
        this.stateMachine.fire(event);
    }

    public State getCurrentState() {
        return this.stateMachine.getState();
    }

    public void printCurrentState() {
        System.out.println("Current State: " + stateMachine.getState());
    }
}
